Skip to content

Let async terminal handlers finish during graceful disconnect - #307

Open
codebanditssss wants to merge 1 commit into
AssemblyAI:masterfrom
codebanditssss:audit/await-async-termination-handlers
Open

codebanditssss wants to merge 1 commit into
AssemblyAI:masterfrom
codebanditssss:audit/await-async-termination-handlers

Conversation

@codebanditssss

Copy link
Copy Markdown

Summary

Let async terminal handlers finish within the remaining graceful-disconnect timeout. Preserve the stop signal and bounded cancellation. Avoid waiting on the write task when disconnect is called by that task. Include nine regression/boundary cases and the required patch-version bump.

The reader sets _stop_event before awaiting Termination or terminal Error handlers. disconnect(terminate=True) wakes on that signal and cancels the read task while a handler is suspended. Normal async with exit is affected.

Reproduction

Register an async Termination handler that waits 100 ms before recording completion. Connect and call disconnect(terminate=True) against a local server that replies with Termination. Baseline cancels the callback in about 2 ms; fixed completes it in about 103 ms. An abnormal 1011 close reproduces the same problem for an async Error handler.

Tests cover suspended handlers, context exit, handler exception isolation/order, nested disconnect, write-side self-wait, slow-handler cancellation and one shared read-side timeout budget.

Validation

  • Full offline unit suite: 1043 passed.
  • WebSockets 11 streaming suites: 226 passed.
  • Real local WebSockets 11.0.3 and 17.0.1: terminal callbacks cancelled before, completed after.
  • Changed-file ruff check and diff whitespace checks pass.
  • The async test file's two existing formatting differences and 15 focused mypy diagnostics match unchanged upstream; whole-project type cleanliness is not claimed.
  • No paid API calls.

Handlers exceeding the timeout can still be cancelled. Cancellation-suppressing handlers can prolong shutdown. This is not a strict whole-disconnect wall-clock bound.

PR #303 concerns unreadable server messages, not cancellation of valid terminal callbacks during shutdown.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant